import React, {Component} from 'react';
import {
  View,
  Text,
  Alert,
  Button,
  ToastAndroid,
  TextInput,
  StyleSheet,
  Dimensions,
  TouchableOpacity,
  ScrollView,
  Image,
} from 'react-native';
import Proposal from './Proposal';
import Jointly from './Jointly';
import Vote from './Vote';
import Check from './Check';
import Execute from './Execute';
// import {getData} from '../../../storage';
import {_getUserInfo, getProposal} from '../../api/index';
import {HeaderLeft} from '../../components';
import {getCount} from '../../api/Proposal';
//import {resolve} from "react-native-svg/src/lib/resolve";
let widthScreen = Dimensions.get('window').width;
let heightScreen = Dimensions.get('window').height;
let token = '';
let userid = '';
export default class ProposalInformation extends Component {
  static navigationOptions = ({navigation}) => ({
    headerTitle: '提案列表',
    headerStyle: {
      backgroundColor: '#fff',
      borderBottomWidth: 0,
      height: 44,
      elevation: 0,
    },
    headerTitleStyle: {
      flex: 1,
      textAlign: 'center',
      color: '#333',
      fontSize: 17,
      fontWeight: '400',
    },
    headerLeft: (
      <HeaderLeft
        triggerBack={() => {
          navigation.goBack();
          // let backFunc = navigation.getParam('backFunc');
          // backFunc.then();
        }}
      />
    ),
    headerRight: <View />,
  });
  constructor(props) {
    super(props);
    //TODO 可以通过我下面这种方式获得这个提案的id，因为没有上一个页面，所以我自己定了一个id，随后你们整合的时候改掉即可
    let id = this.props.navigation.getParam('id');
    //let id = 15;
    this.state = {
      isLoading: true,
      id: id,
    };
  }
  componentDidMount(): void {
    this.getInfo();
    this.getuid();
    this.getdircount();
  }
  async getAll() {
    const [_responseJson1, _responseJson2] = await Promise.all([
      _getUserInfo(),
      getProposal(),
    ]);
    this.setState({
      result1: _responseJson1,
      responseJson2: _responseJson2.data,
    });
  }
  // TODO 修改获取当前用户的id
  async getuid() {
    let responseJson = await _getUserInfo();
    this.setState({
      // isLoading: false,
      result: responseJson,
    });
    userid = this.state.result.uid;
    //alert(this.state.result.uid);
  }
  async getdircount() {
    let sum = await getCount();
    this.setState({
      allDirectorNum: sum.count,
    });
    //alert(this.state.allDirectorNum);
  }
  async getInfo() {
    let param = {id: this.state.id};
    let responseJson = await getProposal(param); //详细信息
    this.setState({
      stage: responseJson.data.stage, //获得不同界面显示的stage
      finish: responseJson.data.finish, //大概是是否结束？
    });
    this.setState({
      title: responseJson.data.title, //提案名称
      createTime: responseJson.data.create_time, //创建时间
      portraitkey: responseJson.data.portraitkey, //创建者头像
      uid: responseJson.data.uid, //创建者的id
      acceptor_portraitkey: responseJson.data.acceptor_portraitkey, //执行者的头像
      acceptor_id: responseJson.data.acceptor_id, //执行者id
      proposal_url: responseJson.data.proposal_url, //文件？
      faqirenName: responseJson.data.faqirenName, //创建者名字
    });
    let deadline = this.getDate(this.state.createTime); //获取截止时间？
    this.setState({
      deadline: deadline,
    });
    let join = []; //TODO 不是未定义 而是空，故改为null
    if (responseJson.data.join !== null) {
      join = JSON.parse(responseJson.data.join);
    }
    this.setState({
      joinList: join,
      // allDirectorNum: 13,
    });
    this.setState({
      voteAgreeUrl: responseJson.data.vote_agree_url,
      voteDisagreeUrl: responseJson.data.vote_disagree_url,
      voteAgreeRes: responseJson.data.vote_agree_res,
      voteDisagreeRes: responseJson.data.vote_disagree_res,
    });
    this.setState({
      executeDocTitle: responseJson.data.execute_doc_title,
      executeDocUrl: responseJson.data.execute_doc_url,
      //role: responseJson.data.role,
    });
    //alert(responseJson.data.execute_doc_url);
    if (this.state.uid === userid) {
      this.setState({
        role: 1,
      });
    } else {
      this.setState({
        role: 0,
      });
    }
    if (responseJson.data.join_res !== undefined) {
      let check = JSON.parse(responseJson.data.join_res);
      this.setState({
        checkList: check,
      });
    }
    this.setState({isLoading: false, refresh: !this.state.refresh});
    //alert(this.state.uid);
  }
  //TODO getDate函数可能广亮那也有，可自行替换
  getDate(date) {
    date += 3 * 30 * 24 * 3600 * 1000;
    date = parseInt(date);
    let d = new Date(date);
    let year = d.getFullYear();
    let month = d.getMonth() + 1;
    let day = d.getDate();

    // if (month + 3 > 12) {
    //   year++;
    //   month = (month + 3) % 12;
    // }
    return year + '/' + month + '/' + day;
  }

  loadJointly() {
    // let token='';
    // // let acceptorId='';
    // await getData('token',function (text) {
    //   token=text;
    //   alert(token);
    // })
    let joinFuc = this.props.navigation.getParam('backFunc');
    let index = this.props.navigation.getParam('index');
    if (!this.state.isLoading) {
      return (
        <Jointly
          stage={this.state.stage}
          allDirectorNum={this.state.allDirectorNum}
          joinList={this.state.joinList}
          proposerUID={this.state.uid}
          proposerAvatarUrl={this.state.portraitkey}
          token={token}
          id={this.state.id}
          joinFunc={() => joinFuc(index)}
          navigation={this.props.navigation}
        />
      );
    } else {
      return <View />;
    }
  }
  loadVote() {
    if (!this.state.isLoading && this.state.stage > 1) {
      return (
        <Vote
          stage={this.state.stage}
          voteAgreeUrl={this.state.voteAgreeUrl}
          voteDisagreeUrl={this.state.voteDisagreeUrl}
          voteAgreeRes={this.state.voteAgreeRes}
          voteDisagreeRes={this.state.voteDisagreeRes}
        />
      );
    } else {
      return <View />;
    }
  }
  //TODO 加了几个内容
  loadExecute() {
    if (!this.state.isLoading && this.state.stage > 2) {
      //alert(this.state.id);
      return (
        <Execute
          id={this.state.id}
          token={token}
          faqirenName={this.state.faqirenName}
          stage={this.state.stage}
          role={this.state.role}
          executeDocTitle={this.state.executeDocTitle}
          executeDocUrl={this.state.executeDocUrl}
        />
      );
    } else {
      return <View />;
    }
  }
  loadCheck() {
    // let token='';
    // let acceptorId='';
    //await getData('token',function (text) {
    //    token=text;
    //})
    let checkFuc = this.props.navigation.getParam('backFunc');
    let index = this.props.navigation.getParam('index');
    if (!this.state.isLoading && this.state.stage > 3) {
      return (
        <Check
          stage={this.state.stage}
          allDirectorNum={this.state.allDirectorNum}
          joinList={this.state.checkList}
          proposerUID={this.state.uid}
          proposerAvatarUrl={this.state.portraitkey}
          token={token}
          id={this.state.id}
          checkFunc={index => checkFuc(index)}
          navigation={this.props.navigation}
        />
      );
    } else {
      return <View />;
    }
  }
  loadSpace() {
    if (this.state.stage > 2) {
      return <View style={{height: heightScreen * 0.325}} />;
    }
  }
  render() {
    if (this.state.isLoading) {
      return <View />;
    }
    return (
      <ScrollView style={{backgroundColor: '#F3F7FA'}}>
        <Proposal
          title={this.state.title}
          deadline={this.state.deadline}
          proposerUID={this.state.uid}
          proposerAvatarUrl={this.state.portraitkey}
          executorUid={this.state.acceptor_id}
          executorAvatarUrl={this.state.acceptor_portraitkey}
          proposalUrl={this.state.proposal_url}
          launchName={this.state.faqirenName}
          navigation={this.props.navigation}
        />
        <View style={lineStyle.longest} />
        {this.loadJointly()}
        <View style={lineStyle.longest} />
        {this.loadVote()}
        <View style={lineStyle.longest} />
        {this.loadExecute()}
        <View style={lineStyle.longest} />
        {this.loadCheck()}
        <View style={lineStyle.longest} />
        {this.loadSpace()}
      </ScrollView>
    );
  }
}
const lineStyle = StyleSheet.create({
  Middle: {
    width: widthScreen * 0.667,
    height: 0.5,
    backgroundColor: '#DCDCDC',
  },
  Middle2: {
    width: widthScreen * 0.792,
    height: 0.5,
    backgroundColor: '#DCDCDC',
  },
  Long: {
    width: widthScreen * 0.84,
    height: 0.5,
    backgroundColor: '#DCDCDC',
  },
  longest: {
    width: widthScreen,
    height: 0.5,
    backgroundColor: '#DCDCDC',
  },
});
